//
//  MainTabBarViewController.m
//  Bees
//
//  Created by Xiaohan on 2018/12/3.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "XHTabBar.h"
#import "XFoundViewController.h"
#import "CXommunityViewController.h"
#import "XWalletViewController.h"
#import "MineViewController.h"
#import "LoginViewController.h"
@interface MainTabBarViewController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC

@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化tabbar
    [self setUpTabBar];
    //添加子控制器
    [self setUpAllChildViewController];
}

#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    //设置背景色 去掉分割线
    [self setValue:[XHTabBar new] forKey:@"tabBar"];
    [self.tabBar setBarTintColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[UIImage new]];//去掉阴影线、
    [self.tabBar setTranslucent:NO];
    [self.tabBar setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}
#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    _VCS = @[].mutableCopy;
    XFoundViewController *found = [XFoundViewController new];
    [self setupChildViewController:found title:XLocalized(@"发现") imageName:@"found_nomal" seleceImageName:@"found_sel"];
    XWalletViewController *Wallet = [XWalletViewController new];
    [self setupChildViewController:Wallet title:XLocalized(@"资产") imageName:@"assets_nomal" seleceImageName:@"assets_sel"];
    CXommunityViewController *chat = [CXommunityViewController new];
    [self setupChildViewController:chat title:XLocalized(@"社群") imageName:@"chat_nomal" seleceImageName:@"chat_sel"];
    MineViewController *mine = [MineViewController new];
    [self setupChildViewController:mine title:XLocalized(@"我的") imageName:@"mine_nomal" seleceImageName:@"mine_sel"];
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //未选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#B5B5B5"],NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateNormal];
    
    //选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#000000"],NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateSelected];
    //包装导航控制器
    RootNavigationController *nav = [[RootNavigationController alloc]initWithRootViewController:controller];
    
    [self addChildViewController:nav];
    [_VCS addObject:nav];
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //    NSLog(@"选中 %ld",tabBarController.selectedIndex);
    
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([tabBarController.childViewControllers indexOfObject:viewController]==2) {
        if (!kCurUser) {
            [XAlertTool alertTitle:XLocalized(@"检测到还未关联BeesID，是否前往关联？") message:XLocalized(@"创建即可使用聊天室及云收藏等功能！") cancelAction:^{
                
            } sureAction:^{
                LoginViewController*login = [LoginViewController new];
                kWeakSelf(tabBarController);
                login.LoginSuccess = ^{
                    weaktabBarController.selectedIndex = 2;
                };
                [kCurrentVC.navigationController hh_pushBackViewController:login];
            }];
            return NO;
        }
    }
    return YES;
}


- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
